package com.iwomy.envpro.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.excel.vo.ErrorMessage;
import com.iwomy.envpro.platform.VO.AirFactorExcelVO;
import com.iwomy.envpro.platform.entity.AirFactorEntity;
import com.iwomy.envpro.platform.entity.BasicTriggerEntity;
import com.iwomy.envpro.platform.entity.DataTemplateDetailsEntity;
import com.iwomy.envpro.platform.exception.PlatformException;
import com.iwomy.envpro.platform.mapper.AirFactorMapper;
import com.iwomy.envpro.platform.service.AirFactorService;
import com.iwomy.envpro.platform.service.BasicTriggerService;
import com.iwomy.envpro.platform.service.DataTemplateDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 监测因子
 *
 * @author pig
 * @date 2023-08-25 14:56:47
 */
@Service
public class AirFactorServiceImpl extends ServiceImpl<AirFactorMapper, AirFactorEntity> implements AirFactorService {
	@Resource
	private BasicTriggerService basicTriggerService;

	@Override
	public R importFactor(List<AirFactorExcelVO> excelVOList, BindingResult bindingResult) {
		List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();
		List<String> collect = excelVOList.stream().map(AirFactorExcelVO::getCnName).collect(Collectors.toList());
		List<AirFactorEntity> list = this.list(Wrappers.<AirFactorEntity>lambdaQuery().in(AirFactorEntity::getCnName, collect));
		List<String> cnNamaList = list.stream().map(AirFactorEntity::getCnName).collect(Collectors.toList());

		excelVOList.stream().forEach(x -> {
			Set<String> errorMsg = new HashSet<>();
			if (cnNamaList.stream().anyMatch(name -> name.equals(x.getCnName()))) {
				errorMsg.add("中文名" + x.getCnName() + "已存在");
			}
			errorMessageList.add(new ErrorMessage(x.getLineNum(), errorMsg));

		});


		if (ObjectUtil.isNotEmpty(list)) {
			return R.failed(errorMessageList);
		}
		return R.ok(this.saveBatch(BeanUtil.copyToList(excelVOList, AirFactorEntity.class)));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateFactor(AirFactorEntity airFactor) {
		AirFactorEntity airFactorEntity = new AirFactorEntity();
		airFactorEntity.setCnName(airFactor.getCnName());
		LambdaQueryWrapper<AirFactorEntity> airFactorEntityLambdaQueryWrapper = Wrappers.lambdaQuery(airFactorEntity);
		AirFactorEntity one = this.getOne(airFactorEntityLambdaQueryWrapper);
		if (ObjectUtil.isNotNull(one) && !airFactor.getId().equals(one.getId()))
			throw new PlatformException("中文名已存在");

		LambdaUpdateWrapper<BasicTriggerEntity> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.set(BasicTriggerEntity::getFactorName, airFactor.getCnName());
		updateWrapper.set(BasicTriggerEntity::getFactorCode, airFactor.getCode());
		updateWrapper.eq(BasicTriggerEntity::getFactorId, airFactor.getId());
		basicTriggerService.update(updateWrapper);
		return this.updateById(airFactor);
	}
}